﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModelDinhMuc.Model;
namespace TinhNguyenVatLieuSanBay
{
    public partial class FrmDungCuPhucVuChuyenBay : Form
    {
        DINHMUCSANBAYDB db = new DINHMUCSANBAYDB();
        public static FrmDungCuPhucVuChuyenBay frmdungcu = null;
        private DungCu currentdungcu;
        IList<ChiTietDungCuPhucVuChuyenBay> lstchitietdungcu;
        private IList<DungCuPhucVuChuyenBay> lstphieudungcu;
        public FrmDungCuPhucVuChuyenBay()
        {
            InitializeComponent();
            frmdungcu = this;
            InitPhieuDungCu();
        }

        private void InitPhieuDungCu()
        {
            lstphieudungcu = db.SelectAllDungCuPhucVuChuyenBay().ExecuteTypedList<DungCuPhucVuChuyenBay>();
            if (lstphieudungcu != null && lstphieudungcu.Count > 0)
            {
                dungCuPhucVuChuyenBayBindingSource.DataSource = lstphieudungcu;
            }
        }

        private void InitChiTietDungCu()
        {
            long idphieudung = long.Parse(gvphieudungcu.CurrentRow.Cells["iDDataGridViewTextBoxColumn"].Value.ToString());
            lstchitietdungcu = db.SelectALLChitietDungCuByPhieuDungCu(idphieudung).ExecuteTypedList<ChiTietDungCuPhucVuChuyenBay>();
            if (lstchitietdungcu != null && lstchitietdungcu.Count > 0)
            {
                chiTietDungCuPhucVuChuyenBayBindingSource.DataSource = lstchitietdungcu;
            }
            else
            {
                chiTietDungCuPhucVuChuyenBayBindingSource.DataSource = null;
            }
        }

        private void InsertUpdateChiTietDungCu()
        {
            long idphieudung = long.Parse(gvphieudungcu.CurrentRow.Cells["iDDataGridViewTextBoxColumn"].Value.ToString());
            bool codinh;
            try
            {
                codinh = bool.Parse(gvchitietdungcu.CurrentRow.Cells["CoDinh"].Value.ToString());
            }
            catch
            {
                codinh = false;
            }
            ChiTietDungCuPhucVuChuyenBay currentchitietdungcu = gvchitietdungcu.CurrentRow.DataBoundItem as ChiTietDungCuPhucVuChuyenBay;
            if (currentchitietdungcu != null)
            {
                IList<ChiTietDungCuPhucVuChuyenBay> lsttmp;
                lsttmp = db.InsertUpdateChiTietPhieuDungCu(idphieudung, currentchitietdungcu.IDDungCu, currentchitietdungcu.SLCapChoKhachVip,
                    currentchitietdungcu.SLCapChoKhachThuong, currentchitietdungcu.SLCapChoPhiCong, currentchitietdungcu.SLCapChoTiepVien, codinh).ExecuteTypedList<ChiTietDungCuPhucVuChuyenBay>();
                if (lsttmp != null && lsttmp.Count > 0)
                {
                    InitChiTietDungCu();
                }
            }
            else
            {
                ChiTietDungCuPhucVuChuyenBay item = new ChiTietDungCuPhucVuChuyenBay();
                item.IDDungCu = long.Parse(gvchitietdungcu.CurrentRow.Cells["iDDungCuDataGridViewTextBoxColumn"].Value.ToString());
                item.IDDungCuPhucVuChuyenBay = idphieudung;
                IList<ChiTietDungCuPhucVuChuyenBay> lsttmp;
                lsttmp = db.InsertUpdateChiTietPhieuDungCu(item.IDDungCuPhucVuChuyenBay, item.IDDungCu, 0,
                    0, 0, 0, codinh).ExecuteTypedList<ChiTietDungCuPhucVuChuyenBay>();
                if (lsttmp != null && lsttmp.Count > 0)
                {
                    InitChiTietDungCu();
                }
            }
        }

        private void gvphieudungcu_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            InitChiTietDungCu();
        }

        private void gvchitietdungcu_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void gvchitietdungcu_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void gvchitietdungcu_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex ==2) 
            {
                string timkiem;
                try
                {
                    timkiem = gvchitietdungcu.CurrentRow.Cells[e.ColumnIndex].Value.ToString();
                }
                catch
                {
                    timkiem = "";
                }
                FrmTimkiemDungCu frm = new FrmTimkiemDungCu(timkiem);
                frm.ShowDialog();
                currentdungcu = frm.dungcu;
                gvchitietdungcu.CurrentRow.Cells[e.ColumnIndex].Value = currentdungcu.TenDungCu;
                gvchitietdungcu.CurrentRow.Cells["iDDungCuDataGridViewTextBoxColumn"].Value = currentdungcu.ID;
                //gvchitietdungcu.CurrentRow.Cells[3].Selected = true;
            }
            if (e.ColumnIndex == 6 || e.ColumnIndex == 3 || e.ColumnIndex == 4 || e.ColumnIndex == 5 || e.ColumnIndex == 7)
            {
                InsertUpdateChiTietDungCu();
            }
        }

        private void FrmDungCuPhucVuChuyenBay_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmdungcu = null;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            var item = gvchitietdungcu.CurrentRow.DataBoundItem as ChiTietDungCuPhucVuChuyenBay;
            if (item != null)
            {
                db.DeleteChiTietDungCu(item.IDDungCu, item.IDDungCuPhucVuChuyenBay).Execute();
                InitChiTietDungCu();
            }
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            IList<DungCuPhucVuChuyenBay> lsttmp;
            lsttmp = lstphieudungcu.Where(x => x.MaMayBay.Contains(txttimkiem.Text)).ToList();
            dungCuPhucVuChuyenBayBindingSource.DataSource = lsttmp;
        }
    }
}
